DevNet Resource Kit Article

CFIMAP

CFIMAP enables ColdFusion developers to access e-mail message information using the IMAP (Internet Message Access Protocol) protocol in much the same way that CFPOP does for the POP3 (Post Office Protocol, version 3) protocol. That is to say, ColdFusion developers can perform the following three basic tasks:

  • Retrieve a list of basic message information for all messages in a specified server and folder: who sent the message, whom it went to, the subject, etc.
  • Retrieve the specific contents of a specified message (or all messages in a folder).
  • Delete a specified message from the IMAP server.

In many ways, IMAP differs from the ubiquitous e-mail access protocol, POP3. In a nutshell, POP was originally designed for offline access of messages. That means a client would intermittently log on to the server and download all the necessary information to be accessed when it no longer was connected to the server. Conversely, IMAP was designed for an online environment; it offers more flexibility in that regard. For more information on IMAP, read "What Is IMAP?" (www.imap.org/about/whatisIMAP.html); for a comparison between IMAP and POP, read "Comparing Two Approaches to Remote Mailbox Access: IMAP vs. POP" (www.imap.org/papers/imap.vs.pop.brief.html).

CFIMAP works out of the box to handle the basic functions listed above—in an almost identical way to CFPOP. It is written using the JavaMail API that is included with 'the underlying J2EE architecture in ColdFusion MX. This means that with a little Java knowledge you can customize the CFIMAP tag to fit your specific needs.

Installing CFIMAP

It's relatively straightforward to install CFIMAP:

  1. In the ZIP file, extract the contents of the /WEB-INF folder to the WEB-INF folder in your ColdFusion installation. In stand-alone mode, this is cfmx_root/wwwroot/WEB-INF/. In the J2EE edition, this is server_root/WEB-INF.
  2. Extract the /tags folder to the custom tag directory.
  3. Extract test.cfm to a web location. This tag is optional and for testing purposes only.
  4. Restart the ColdFusion server to register the Java class library.

Using CFIMAP

Anyone who has ever used CFPOP before will find CFIMAP quite familiar. After all, they really perform the same functions, albeit using different e-mail protocols. Let's take a look at how CFIMAP handles its three major actions. The key difference is the addition of the FOLDER attribute, which allows you to access messages from specified folders. The use of folders and public folders is a core strength of IMAP.

ACTION: getHeaderOnly

Use the getHeaderOnly action when you wish to retrieve a ColdFusion query object, including the primary information about the e-mails—subject, whom it went to, whom it came from, and the like. Because this information is stored in e-mail headers, and because the actual text of the message will not be retrieved, this action is referred to as getHeaderOnly.

Code example:

<cf_imap server="localhost" username="me" password="mypass" action="getHeaderOnly" folder="publicMail" name="messageList"> 

Assuming there are messages in the inbox of the specified user, this tag call returns a ColdFusion query object that you easily loop through and output. (See the cf_imap tag API section later for detailed information on the query object that is returned.)

ACTION: getAll

Use the getAll action when you wish to retrieve a ColdFusion query object, including not just the primary information about the e-mails (as described previously) but also the message contents itself. If a message number is given, it will extract only the message with that corresponding message number.

Code example:

<cf_imap server="localhost" username="me" password="mypass" action="getAll" messagenumber="101" name="messageList" attachmentPath="folder_location">

Assuming there are messages, this tag call returns a ColdFusion query object that you can easily loop through and output. (See the cf_imap tag API section below for detailed information on the query object that is returned.)

ACTION: delete

Use the delete action to delete the message specified by the messagenumber attribute from the IMAP server.

Code example:

<cf_imap server="localhost" username="me" password="mypass" action="delete" messagenumber="101">


The cf_imap Tag API

The cf_imap custom tag retrieves message information from an IMAP server.

Retrieves or deletes e-mail messages from an IMAP mail server.

Syntax:

<cf_imap 
server = "servername"
port = "port_number" folder= "folder_name"
username = "username"
password = "password"
action = "action"
name = "queryname"
messageNumber = "number">

Attribute Req/Opt Default Description
server Required  

IMAP server identifier:

  • A host name; for example, biff.upperlip.com

An IP address; for example, 192.1.2.225

port Optional 143 IMAP port
folder Optional Inbox (user's) Used with actions getHeaderOnly and getAll to specify folder from which to return message information
username Optional   A username
action Optional getHeaderOnly getHeaderOnly: returns message header information only
getAll: returns message header information, message text
delete: deletes messages on POP server
name Required if action = "getAll" or "getHeaderOnly"   Name for query object that contains the retrieved message information
messageNumber Required if action= "delete" Optional for action = "getAll"   Message number or comma-delimited list of message numbers to get or delete (invalid message numbers are ignored)
attachmentPath Optional   Folder path to write the attachments to (files with the same names are overwritten)

The cf_imap tag—just as the cfpop tag—retrieves one or more mail messages from an IMAP server and populates a ColdFusion query object with the resulting messages, one message per row. Alternatively, it deletes one or more messages from the IMAP server.

To optimize performance, two retrieve options are available ( ACTION="getHeaderOnly" and ACTION="getAll" ) . Message header information is typically short and therefore quick to transfer. Because message text and attachments can be very long, they take longer to process.

cf_imap Query Variables

The following table describes the variables that provide information about the query that is returned by cf_imap.

Variable names Description
queryname.recordCount Number of records returned by the query
queryname.currentRow Current row that cfoutput is processing
queryname.columnList List of column names in the query

Query Message Header and Body Columns

The following table lists the message header and body columns that are returned if action = "getHeaderOnly" or "getAll".

Column name getHeaderOnly returns getAll returns
queryname.date yes yes
queryname.from yes yes
queryname.messagenumber yes yes
queryname.replyto yes yes
queryname.subject yes yes
queryname.cc yes yes
queryname.to yes yes
queryname.body n/a yes
queryname.textBody n/a yes
queryname.HTMLBody n/a yes
queryname.header n/a yes
queryname.attachments n/a yes
queryname.attachmentfiles n/a yes

If the mail message includes a part with a Content-Type of text/plain, the queryname.textBody column contains the part's message content. If the mail message includes a part with a Content-Type of text/HTML, the queryname.HTMLBody column contains the part's message content. If no Content-Type matches these types, the columns are empty. The queryname.Body column always contains the first message body found.

If the message contains attachments, queryname.attachments will contain a tab-delimited list of attachment filenames and queryname.attachmentfiles will contain the directory location. This only happens if the attachments are written to file when the attachmentPath attribute is specified with a valid directory location.

To create a ColdFusion date/time object from the date-time string that is extracted from a mail message in the queryname.date column, use the following table.

Locale How to create a ColdFusion date/time object from queryname.date
English (US) Use the ParseDateTime function (the pop-conversion attribute is not needed for cf_imap )
Other Extract the date part of string and pass it to the LSParseDateTime function

Note: To set the default display format of date, time, number, and currency values, use the SetLocale function.